package io.gitee.chearnee.fw.sidecar.action;

import io.gitee.chearnee.fw.sidecar.application.ApplicationRegister;
import io.gitee.chearnee.fw.sidecar.application.ServiceInfo;
import io.gitee.chearnee.fw.sidecar.client.ClientHelper;
import io.gitee.chearnee.fw.sidecar.model.ApplicationRes;
import io.gitee.chearnee.fw.sidecar.model.BaseApplication;
import io.gitee.chearnee.fw.sidecar.util.IPUtils;
import io.gitee.chearnee.fw.sidecar.workshop.ActionConsumer;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 12/19/2022 1:53 PM
 */
@RequiredArgsConstructor
public class BootstrapActionHelper implements ApplicationRunner {

    private final List<ApplicationRegister> applicationRegister;
    private final ServiceInfo serviceInfo;
    private final ClientHelper clientHelper;

    @Override
    public void run(ApplicationArguments args) {
        BaseApplication baseClientReq = new BaseApplication();
        baseClientReq.setApplicationsMap(applicationRegister.stream().collect(Collectors.toMap(ApplicationRegister::getAppId, Function.identity())));
        baseClientReq.setServiceName(serviceInfo.getServiceName());
        baseClientReq.setIp(IPUtils.getCurrentIpAddress());
        List<ApplicationRes> applicationRes = clientHelper.registerApplications(baseClientReq);

        ActionConsumer.timeExec();
    }
}
